package com.project.mall.controller;


import com.alibaba.fastjson.JSON;
import com.project.mall.pojo.Goods;
import com.project.mall.pojo.Secondtype;
import com.project.mall.pojo.User;
import com.project.mall.service.ServiceL;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/afters")
public class ControllerL {

    /*产品管理-产品列表*/
    @Resource
    private ServiceL serviceL;
    /*------------------------------------------分割线---------------------------------------------*/

    /*查询Secondtype信息*/
    @RequestMapping("AllSecondtype")
    public String allSecondtype(Model model){
        List<Secondtype> allsecondtype=serviceL.allSecondtype();
        model.addAttribute("allsecon",allsecondtype);
        return "after/product-add";
    }
    /*------------------------------------------分割线---------------------------------------------*/

    /*添加产品管理信息*/
    @PostMapping("/doproductadd")
    public String doproductadd(Goods goods){
        boolean b = serviceL.addGoods(goods);
        if (b){
            return "redirect:/after/productlist";
        }else {
            return "after/productadd";
        }
    }
    /*------------------------------------------分割线---------------------------------------------*/

    /*删除产品管理的信息*/
    @GetMapping("/deleproductlist/{gid}")
    @ResponseBody
    public String deleproductlist(@PathVariable("gid") Integer gid){
        boolean dele = serviceL.delectGoods(gid);
        if (dele){
            return JSON.toJSONString(dele);
        }else {
            return "false";
        }
    }

 /*------------------------------------------分割线---------------------------------------------*/

    /*根据id查询到产品的相关信息*/
    @GetMapping("/toproductupdate/{gid}")
    public String toproductupdate(@PathVariable("gid") Integer gid, Model model){
       Goods allgoodsId=serviceL.allGoodsId(gid);
       model.addAttribute("toupdate",allgoodsId);
        return "after/product-update";
    }

    /*修改产品信息*/
    @PostMapping("/doproductupdate")
    public String d(Goods goods){
        boolean isUpdate = serviceL.updataGoods(goods);
        if(isUpdate){
            return "redirect:/after/productlist";
        }
        return "after/product-update";
    }
    /*------------------------------------------分割线---------------------------------------------*/

    /*异步请求批量删除的Goods  根据id进行批量删除*/
    @GetMapping("/delectgoodss/{gid}")
    @ResponseBody
    public String delectgoodss(@PathVariable("gid") String gid){
        String a = gid;
//        boolean delelistss = serviceL.delectssGoods(gid);
//        if (delelistss){
//            return JSON.toJSONString(delelistss);
//        }else {
//            return "productlist";
//        }
        return null;
    }

    /*------------------------------------------分割线---------------------------------------------*/

    /*添加二级栏目信息*/
    @PostMapping("/doadd")
    public String addsecondtype(Secondtype secondtype){
        boolean addsecon = serviceL.addSecondtype(secondtype);
        System.out.println(addsecon);
        if (addsecon){
            return "redirect:/after/systemcategory";
        }else {
            return "after/systemcategoryadd";
        }
    }

    /*------------------------------------------分割线---------------------------------------------*/

    /*根据id删除二级栏目信息*/
    @GetMapping("delectsecondtype/{secid}")
    @ResponseBody
    public String delectsecondtype(@PathVariable("secid") Integer secid){
        boolean delesecon = serviceL.deleteSecondtype(secid);
        Map<String,Boolean> map=new HashMap<>();
        if(delesecon){
            map.put("msg",true);
        }else {
            map.put("msg",false);
        }
        return JSON.toJSONString(map);
    }

    /*------------------------------------------分割线---------------------------------------------*/

    /*根据id查询到相关的Secondtype的信息*/
    @GetMapping("tosecondtype/{secid}")
    public String tosecondtype(@PathVariable("secid") Integer secid, Model model){
        Secondtype secondtype = serviceL.allSecondtypeId(secid);
        model.addAttribute("updas",secondtype);
        return "after/system-category-update";
    }

    /*根据id进行栏目的修改信息*/
    @PostMapping("/doupdatesecon")
    public String updatesecon(Secondtype secondtype){
        boolean updatesecondtype = serviceL.updataSecondtype(secondtype);
        if (updatesecondtype){
            return "redirect:/after/systemcategory";
        }
        return "after/system-category-update";
    }
/*------------------------------------------分割线---------------------------------------------*/
    /*根据id删除评论功能信息*/
    @GetMapping("/delectcomment/{coid}")
    @ResponseBody
    public String delectcomment(@PathVariable("coid") Integer coid){
         boolean delecomment= serviceL.deleComment(coid);
         Map<String,Boolean> map=new HashMap<>();
         if(delecomment){
             map.put("msg",true);
         }else {
             map.put("msg",false);
         }
         return JSON.toJSONString(map);

    }


    /*------------------------------------------分割线---------------------------------------------*/

    /*（修改）User表 根据id先查询到相关信息*/
    @GetMapping("toalluserbyid/{userid}")
    public String toalluserbyid(@PathVariable("userid") Integer userid, Model model){
        User user = serviceL.allUserById(userid);
        model.addAttribute("alluser",user);
        return "after/member-update";
    }

    /*(修改) User表 */
    @PostMapping("/doupdateuser")
    public String doupdateuser(User user){
        boolean updateUser = serviceL.updateUser(user);
        if (updateUser){
            return "redirect:/after/memberlist";
        }
        return "after/member-update";

    }

    /*------------------------------------------分割线---------------------------------------------*/

    /*删除User表的信息 根据id进行删除*/
    @GetMapping("/delectuser/{userid}")
    @ResponseBody
    public String delectuser(@PathVariable("userid") Integer userid){
        boolean delectuser = serviceL.delectUser(userid);
        Map<String,Boolean> map=new HashMap<>();
        if (delectuser){
            map.put("msg",true);
        }else {
            map.put("msg",false);
        }
        return JSON.toJSONString(map);
    }

    /*------------------------------------------分割线---------------------------------------------*/
    /*根据id来进行删除意见反馈表的信息*/
    @GetMapping("/deletefeedback/{backid}")
    @ResponseBody
    public String deletefeedback(@PathVariable("backid") Integer backid){
        boolean deletefeedbackid = serviceL.deleteFeedbakcById(backid);
        Map<String,Boolean> map=new HashMap<>();
        if (deletefeedbackid){
            map.put("msg",true);
        }else {
            map.put("msg",false);
        }
        return JSON.toJSONString(map);
    }


    /*------------------------------------------分割线---------------------------------------------*/

    //输入login跳转到login页面
    @RequestMapping(value="/login",method= RequestMethod.GET)
    public String login(){
        return "after/login";
    }

    /**
     *   提交登录界面，判断登录信息正确则重定向到首页
     *      否则返回登录页面
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpSession session, HttpServletRequest req, Model model) throws UnknownHostException {
        String userName=req.getParameter("userName").trim();
        String password=req.getParameter("passwordes");
        /*
         * 根据用户账户获取用户密码
         */
        System.out.println(userName);
        String pwd=serviceL.pwd(userName);
        System.out.println(pwd);
        if(pwd==null){
            System.out.println("无该账户");
            model.addAttribute("errormess", "无该账户!");
            /* req.setAttribute("errormess","无该账户!");*/
            return "after/login";
        }else if(password.equals(pwd)){
            System.out.println("登陆成功！！");
            /*service.*/
            session.setAttribute("phone", userName);
            System.out.println(userName);
            return "after/index";
        }
        return "after/login";
    }



//    /*根据id查询到相关要修改的信息*/
//    @GetMapping("toupdateadmin/{adminid}")
//    public String toupdateadmin(@PathVariable("adminid") Integer adminid,Model model){
//        Admin allupdate = serviceL.alladminss(adminid);
//        model.addAttribute("allupdate",allupdate);
//        return "after/change-password";
//    }
//    /*根据id查询到的进行修改*/
//    @PostMapping("/doupdateadmin")
//    public String doupdateadmin(Admin admin){
//        boolean updateadmin = serviceL.updateadmin(admin);
//        if (updateadmin){
//            return "redirect:/after/index";
//        }
//        return "after/change-password";
//    }



    /*模糊查询根据产品名字*/
    @RequestMapping("/like")
    public String like(@RequestParam String gname, Model model){
        List<Goods> likegoods = serviceL.likegoods(gname);
        if(likegoods.size()==0){
            return "after/product-list";
        }
        model.addAttribute("like",likegoods);
        return "after/product-like";
    }

    /*模糊查询 根据二级分类名字*/
    @RequestMapping("/likesec")
        public String likesec(@RequestParam String secname, Model model){
        List<Secondtype> secondtypes = serviceL.likeSecondtype(secname);
        if(secondtypes.size()==0){
            return "after/system-category";
        }
        model.addAttribute("likesecdtype",secondtypes);
        return "after/system-like";
    }











}
